package com.kly.user.db.dao.impl;
import com.fqgj.common.base.AbstractBaseMapper;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.StringUtils;
import com.google.api.client.util.Lists;
import com.google.common.collect.Maps;
import com.kly.user.db.dao.UserTagDao;
import com.kly.user.db.dbo.UserTagDBO;
import com.kly.user.db.entity.UserTagEntity;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;

/**
* User: linqu
* Date: 2024-01-03
* Time: 15:59:08.440
*/

@Repository
public class UserTagDaoImpl extends AbstractBaseMapper<UserTagEntity> implements UserTagDao {

    @Override
    public UserTagEntity add(String userCode, String tagType, Integer tagCode) {
        final UserTagEntity iWant = getUserTag(userCode, Integer.valueOf(tagType));
        if (iWant == null) {
            UserTagEntity o = new UserTagEntity();
            o.setUserCode(userCode);
            o.setTagType(Integer.valueOf(tagType));
            o.setTagCode(tagCode);
            this.insert(o);
            return o;
        }
        iWant.setTagCode(tagCode);
        this.updateByPrimaryKey(iWant);
        return iWant;
    }

    @Override
    public List<UserTagEntity> batchAdd(String userCode, String tagType, List<Integer> values) {
        if (CollectionUtils.isEmpty(values)) {
            return null;
        }
        // 删除老数据
        removeUserTagByTagType(userCode, tagType);
        if (CollectionUtils.isEmpty(values)) {
            return null;
        }

        // 添加新数据
        List<UserTagEntity> userList = Lists.newArrayList();
        for (Integer tagCode : values) {
            UserTagEntity o = new UserTagEntity();
            o.setUserCode(userCode);
            o.setTagType(Integer.valueOf(tagType));
            o.setTagCode(tagCode);
            userList.add(o);
        }
        this.getSqlSession().insert(this.getStatement("batchAdd"), userList);

        return userList;
    }

    @Override
    public List<UserTagDBO> listUserTag(String userCode) {
        if (StringUtils.isEmpty(userCode)) {
            return null;
        }
        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        params.put("deleted", 0);
        return this.getSqlSession().selectList(this.getStatement("listUserTag"), params);
    }

    private void removeUserTagByTagType(String userCode, String tagType) {
        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        params.put("tagType", tagType);
        params.put("deleted", 0);
        this.getSqlSession().delete("removeByUserCodeAndType", params);
    }

    private UserTagEntity getUserTag(String userCode, Integer tagType) {
        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("userCode", userCode);
        params.put("tagType", tagType);
        params.put("deleted", 0);
        final List<UserTagEntity> userTagEntities = selectByParams(params);
        if (CollectionUtils.isEmpty(userTagEntities)) {
            return null;
        }
        return userTagEntities.get(0);
    }



}